import {
    PROP_GS_TYPE
} from "./GameGSDefine";
import SodaCommon from "../soda/SodaCommon";
import SodaEventSoda from "../soda/SodaEventSoda";
import SodaKey from "../soda/SodaKey";
import {
    CodeString,
    CodeNum
} from "../soda/SodaCode";
import {
    IconData
} from "../ts/ModuleData";

var GSSceneGame = cc.Class({
    extends: require('BaseGSScene'),

    statics: {
        instance: null,
    },

    properties: {
        pCellGeZiItem: cc.Prefab,
        pCellLizi: cc.Prefab,
        pFenShu: cc.Prefab,
        pCCCombo: cc.Prefab,
        pTongGuan: cc.Prefab,
        labSoda: cc.Label,
        nodProp: cc.Node,
        openExchange: cc.Node,
        openSoda: cc.Node,
        wenHao: cc.Node,
        iconItem: cc.Node,
    },

    onLoad() {
        this.cnmd();
        GSSceneGame.instance = this;
        this.TMInitPool();
        this.initSoda();
        this.IchushiHua();
        this.ItmdJianTing();
        this.initItemIcon();
    },

    start() {
        F.GSPartner.sendEvent('场景', {
            '类型': 'game'
        });
        F.Game.gameAudioGS.playBgm();
    },

    IchushiHua() {
        SodaEventSoda.on(SodaEventSoda.USER_ADD_INTEGRAL, this._initSoda, this);
        SodaEventSoda.on(SodaEventSoda.USER_EXCHANGE_SUCC, this._userExcSucc, this);
        SodaEventSoda.on(SodaEventSoda.ERROR, this._userError, this);
    },

    ItmdJianTing() {
        this.openExchange.on("click", () => {
            F.Ui.openUI('PanelBindPhone');
        });

        this.openSoda.on("click", () => {
            SodaCommon.openMG({
                appId: SodaKey.SODA_APP_ID
            });
        });

        this.wenHao.on("click", () => {
            F.Ui.openUI('PaneWenHao');
        });
    },

    initItemIcon() {
        let itemObj = this.iconItem.getComponent("IconItem");
        let data = new IconData();
        data.iconUrl = "logo";
        data.iconName = "汽水易购";
        itemObj.setData(data);
    },

    _userExcSucc(eventName, params) {
        if (params) {
            F.Game.toast(`恭喜兑换积分:${params.data}`, 1);
        }
        this._initSoda();
    },

    _userError(eventName, params) {
        if (params) {
            F.Game.toast(`${CodeString[params.code]}`, 1);
        }
    },

    _initSoda(evenName, res) {
        if (cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME) {
            this.labSoda.string = `${SodaCommon.userData.integral}`;
            if (res) {
                console.log(JSON.stringify(res));
                if (res.code === CodeNum.CD_OK) {
                    F.Game.toast(`恭喜获得积分:${res.data}`, 1);
                } else {
                    F.Game.toast(`${CodeString[res.code]}`, 1);
                }
                SodaCommon.vibrateShort();
            }
        } else {
            this.labSoda.string = `999`;
            // F.Game.toast(`恭喜获得积分:999`, 1);
        }
    },

    onDestroy() {
        SodaEventSoda.off(SodaEventSoda.USER_ADD_INTEGRAL, this._initSoda, this);
    },

    initSoda() {
        let useData = SodaCommon.userData;
        if (useData) {
            this.labSoda.string = `${useData.integral}`;
        }
    },

    // 初始化节点池
    TMInitPool() {
        F.Utils.createPool('nodePoolCell', this.pCellGeZiItem, 5);
        F.Utils.createPool('nodePoolParticle', this.pCellLizi, 5);
        F.Utils.createPool('nodePoolScore', this.pFenShu, 5);
        F.Utils.createPool('nodePoolCombo', this.pCCCombo, 5);
        F.Utils.createPool('nodePoolPassTip', this.pTongGuan, 1);
    },

    FunMudPListener(button) {
        let name = button.node.name;
        if (name == "btnSBBack") {
            F.Ui.openUI('PanelBack')
        }
    },

    // update (dt) {},
});